﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Keeyo.DataHelper;
using System.Reflection.Emit;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;

namespace Test
{
    class Program
    {
        public delegate HTMLEntity HelloWorldDelegate();

        static void Main(string[] args)
        {

            //Console.WriteLine(BuildMethodaa().Invoke().DataType);
            //delete();
            Vincent();

            Console.ReadLine();

        }

        private static void Vincent()
        {
            string mysqlConn = "User Id=test;Host=www.wajiong.com;Database=test;password=test0.123";
            VincentEntity ve = new VincentEntity();
            IQuery<VincentEntity> query = DataUtility.GetAdapter<VincentEntity>(mysqlConn);
            ve.name = DateTime.Now.ToString();
            query.Insert(ve);
            query.Delete(new int[] { ve.ID });
            Console.WriteLine(ve.ID);



        }

        public static void mmx()
        {
            IQuery<HTMLEntity> query = DataUtility.GetAdapter<HTMLEntity>("Server=127.0.0.1;UID=sa;PWD=mM);DataBase=AnalyHTML;Connect Timeout=90");
            //Console.Write(query.SelectID("7D12D064-DAF8-47B1-AFC1-004BA611C330").ID);
            //query.Include = "DataType,ID,Url";
            //query.Exclude = "DataType";
            IPagingList<HTMLEntity> list = query.SelectAll(" [DataType]=2 ", null, 1, 10);
            Console.WriteLine(list.TotalItems);
            foreach (HTMLEntity item in list)
            {
                Console.WriteLine(item.DataType);
                Console.WriteLine(item.ID);
                Console.WriteLine(item.Brand.ID);
            }

            Console.WriteLine(query.GetSelectString());
            Console.WriteLine(query.GetInsertString());
            Console.ReadLine();
        }

        public HTMLEntity mmx1()
        {
            HTMLEntity a = new HTMLEntity();
            //a.DataType = DataType.b;
            return a;
        }

        public int a(int b)
        {
            Console.Write(b);
            return b;
        }

        public static void delete()
        {
            IQuery<HTMLEntity> query = DataUtility.GetAdapter<HTMLEntity>("Server=127.0.0.1;UID=sa;PWD=mM);DataBase=AnalyHTML;Connect Timeout=90");
            List<Guid> ids = new List<Guid>();
            ids.Add(Guid.NewGuid());
            Console.Write(query.Delete<Guid>(ids.ToArray()));
        }

        public static HTMLEntity aa(IDataReader a)
        {
            HTMLEntity b = new HTMLEntity();
            //int i = 0;
            //a.IsDBNull
            //b.DataType = (DataType)Convert.ToInt32(a.GetValue(i).ToString());
            b.Brand = new BrandEntity();
            b.Brand.ID = a.GetGuid(12);
            return b;
        }

        public static HelloWorldDelegate BuildMethodaa()
        {

            DynamicMethod method = new DynamicMethod("HelloWorld", typeof(HTMLEntity), Type.EmptyTypes);
            // Preparing Reflection instances
            ConstructorInfo ctor1 = typeof(HTMLEntity).GetConstructor(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new Type[]{
            },
                null
                );
            ConstructorInfo ctor2 = typeof(System.Nullable<>).MakeGenericType(typeof(DataType)).GetConstructor(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new Type[]{
            typeof(DataType)
            },
                null
                );
            MethodInfo method3 = typeof(HTMLEntity).GetMethod(
                "set_DataType",
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new Type[]{
            typeof(System.Nullable<>).MakeGenericType(typeof(DataType))
            },
                null
                );

            // Adding parameters
            ILGenerator gen = method.GetILGenerator();
            // Preparing locals
            LocalBuilder b = gen.DeclareLocal(typeof(HTMLEntity));
            LocalBuilder c = gen.DeclareLocal(typeof(HTMLEntity));
            // Preparing labels
            Label label24 = gen.DefineLabel();
            // Writing body
            gen.Emit(OpCodes.Nop);
            gen.Emit(OpCodes.Newobj, ctor1);
            gen.Emit(OpCodes.Stloc_0);
            gen.Emit(OpCodes.Ldloc_0);
            gen.Emit(OpCodes.Ldc_I4_1);
            gen.Emit(OpCodes.Newobj, ctor2);
            gen.Emit(OpCodes.Callvirt, method3);
            gen.Emit(OpCodes.Nop);
            gen.Emit(OpCodes.Ldloc_0);
            gen.Emit(OpCodes.Stloc_1);
            gen.Emit(OpCodes.Br_S, label24);
            gen.MarkLabel(label24);
            gen.Emit(OpCodes.Ldloc_1);
            gen.Emit(OpCodes.Ret);
            // finished
            HelloWorldDelegate HelloWorld = (HelloWorldDelegate)method.CreateDelegate(typeof(HelloWorldDelegate));
            return HelloWorld;
        }




    }
}
